import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/a.dart';
import 'package:finance_app/src/common/app_colors.dart';
import 'package:finance_app/src/widgets/bottom_sheet/list_select_bottom_sheet.dart';
import 'package:finance_app/src/widgets/pop/drow_down_menu.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';

class NormalBorderPop<T> extends StatefulWidget {
  final T select;
  final List<T> group;
  final String hint;
  final ValueChanged<T> onSelect;
  final bool isTop;
  final bool showShadow;
  final bool enable;
  final double horizontalPadding;
  final BuildDisplayName<T> buildDisplayName;
  final double height;

  const NormalBorderPop(
      {Key key,
      this.horizontalPadding,
      this.select,
      this.group,
      this.hint,
      this.onSelect,
      this.height,
      this.isTop = false,
      this.showShadow = false,
      this.enable = true,
      this.buildDisplayName})
      : super(key: key);

  @override
  _NormalBorderPopState<T> createState() => _NormalBorderPopState<T>();
}

class _NormalBorderPopState<T> extends State<NormalBorderPop<T>> {
  DropDownMenuController controller = DropDownMenuController();
  T _select;

  @override
  void initState() {
    super.initState();
    _select = widget.select;
    controller.addListener(() {
      setState(() {});
    });
  }

  @override
  void didUpdateWidget(covariant NormalBorderPop oldWidget) {
    if (widget.select != _select) {
      setState(() {
        _select = widget.select;
      });
    }
    super.didUpdateWidget(oldWidget);
  }

  @override
  void dispose() {
    super.dispose();
    controller.dispose();
  }

  Widget _buildIndustryItem(T e) {
    return PressedOpacityButton(
      onPressed: () {
        widget.onSelect?.call(e);
        controller.close();
        setState(() {
          _select = e;
        });
      },
      color: e == _select ? aceWhiteGreyColor : null,
      padding: EdgeInsets.symmetric(horizontal: getDp(15), vertical: getDp(19)),
      child: Text(
        widget.buildDisplayName?.call(e),
        style: TextStyle(
          color: aceBlackColor,
          fontSize: getSp(16),
          fontWeight: FontWeight.w700,
          height: 1,
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    Widget child = buildSelectInput(isPop: controller.isOpen);
    if (controller.isOpen) {
      child = WillPopScope(
        onWillPop: () async {
          controller.close();
          return false;
        },
        child: child,
      );
    }
    return Padding(
      padding: EdgeInsets.symmetric(
        horizontal: widget.horizontalPadding ?? getDp(35),
      ),
      child: AbsorbPointer(
        absorbing: !widget.enable ?? false,
        child: DropDownMenu(
          isTop: widget.isTop,
          isStackChild: true,
          controller: controller,
          dropDownMenu: Container(
            clipBehavior: Clip.antiAliasWithSaveLayer,
            decoration: BoxDecoration(
              color: Colors.white,
              border: Border.all(
                color: aceGrayColor,
                width: getDp(0.5),
              ),
              borderRadius: BorderRadius.circular(getDp(15)),
            ),
            child: Column(
              children: [
                if (widget.isTop == true) ...[
                  Container(
                    constraints: BoxConstraints(
                      maxHeight: getDp(280),
                    ),
                    child: ListView.builder(
                      shrinkWrap: true,
                      padding: EdgeInsets.zero,
                      itemCount: widget.group.length,
                      itemBuilder: (BuildContext context, int index) =>
                          _buildIndustryItem(widget.group[index]),
                    ),
                  ),
                  Divider(
                    height: 1,
                  ),
                  PressedOpacityButton(
                      onPressed: () {
                        controller.close();
                      },
                      child: buildSelectInput(isPop: true)),
                ] else ...[
                  PressedOpacityButton(
                      onPressed: () {
                        controller.close();
                      },
                      child: buildSelectInput(isPop: true)),
                  Divider(
                    height: 1,
                  ),
                  Container(
                    constraints: BoxConstraints(
                      maxHeight: getDp(280),
                    ),
                    child: ListView.builder(
                      shrinkWrap: true,
                      padding: EdgeInsets.zero,
                      itemCount: widget.group.length,
                      itemBuilder: (BuildContext context, int index) =>
                          _buildIndustryItem(widget.group[index]),
                    ),
                  )
                ]
              ],
            ),
          ),
          child: child,
        ),
      ),
    );
  }

  Widget buildSelectInput({bool isPop = false}) {
    return Container(
        padding: EdgeInsets.only(left: getDp(14), right: 18),
        height: widget.height ?? getDp(54),
        decoration: BoxDecoration(
          color: Colors.white,
          border: widget.showShadow
              ? null
              : isPop
                  ? null
                  : Border.all(
                      color: aceGrayColor,
                      width: getDp(0.5),
                    ),
          borderRadius: isPop
              ? widget.isTop == true
                  ? BorderRadius.only(
                      bottomLeft: Radius.circular(getDp(15)),
                      bottomRight: Radius.circular(getDp(15)),
                    )
                  : BorderRadius.only(
                      topLeft: Radius.circular(getDp(15)),
                      topRight: Radius.circular(getDp(15)),
                    )
              : BorderRadius.circular(getDp(15)),
          boxShadow: widget.showShadow
              ? [
                  BoxShadow(
                    color: Colors.black.withOpacity(0.25),
                    blurRadius: getDp(22),
                    spreadRadius: -getDp(5),
                  ),
                ]
              : null,
        ),
        child: Row(
          children: [
            Expanded(
                child: _select == null
                    ? Text(
                        widget.hint ?? '',
                        style: TextStyle(
                          color: !widget.enable ? aceBlackColor : aceBlueColor,
                          fontSize: getSp(14),
                          fontWeight: FontWeight.w700,
                          height: 16 / 14,
                        ),
                      )
                    : Text(
                        widget.buildDisplayName?.call(_select) ?? '',
                        style: TextStyle(
                          color: !widget.enable ? aceBlackColor : aceBlueColor,
                          fontSize: getSp(16),
                          fontWeight: FontWeight.w700,
                          height: 1,
                        ),
                      )),
            SvgPicture.asset(
              A.assetsImagesStyleArrowBlack,
              width: getDp(8),
              height: getDp(8),
              fit: BoxFit.contain,
              color: !widget.enable ? aceBlackColor : aceBlueColor,
            ),
          ],
        ));
  }
}
